<template>
  <login-canvas />
  <div class="login-container">
    <div class="login-sider-container">
      <div class="sider-cent">
        <div class="sider-header">
          <img src="https://vitejs.cn/logo.svg" alt />
          <img src="https://vue3js.cn/docs/logo.png" alt />
          <img class="eleplus" src="../../assets/images/eleplus.svg" alt />
        </div>
        <el-form :rules="loginRules" :model="userInfo" ref="loginForm">
          <el-form-item prop="username">
            <span @click="$refs.userNameRef.focus()" :class="nameClass">请输入账号/手机号</span>
            <el-input
              v-model.trim="userInfo.username"
              @focus="habdleUserName(true)"
              @blur="habdleUserName(false)"
              ref="userNameRef"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <span @click="$refs.passWordRef.focus()" :class="passClass">请输入密码</span>
            <el-input
              v-model.trim="userInfo.password"
              ref="passWordRef"
              @focus="habdleUserPass(true)"
              @blur="habdleUserPass(false)"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>
              登录表示您已同意
              <a style="color: #409eff; cursor: pointer">《服务条款》</a>
            </div>
            <el-button type="primary" @click="submitForm" class="submit-btn">立即登录</el-button>
            <div>默认账号: admin 密码:123456</div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, toRefs, onMounted } from 'vue'
import Message from '../../plugins/global'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import LoginCanvas from '../../components/LoginCanvas.vue'
export default {
  components: {
    LoginCanvas,
  },
  setup() {
    // 表单的ref
    const Router = useRouter()
    const loginForm = ref(null)
    const store = useStore()
    const state = reactive({
      userInfo: {
        username: '',
        password: '',
      },
      // 效验规则
      loginRules: {
        username: [
          { required: 'true', message: '账号不能为空', trigger: 'blur' },
        ],
        password: [
          { required: 'true', message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 12, message: '请填写正确密码长度', trigger: 'blur' },
        ],
      },
    })
    // 输入框placeholder的动态class
    const nameClass = ref('')
    const passClass = ref('')
    const habdleUserName = (flag) => {
      if (flag) nameClass.value = 'name-right'
      else {
        const { username } = state.userInfo
        if (!username) nameClass.value = ''
      }
    }
    const habdleUserPass = (flag) => {
      if (flag) passClass.value = 'pass-right'
      else {
        const { password } = state.userInfo
        if (!password) passClass.value = ''
      }
    }

    // 点击登录按钮
    const submitForm = async () => {
      const { username, password } = state.userInfo
      // 登录前验证
      loginForm.value.validate((valid) => {
        if (valid) {
          const data = {
            params: {
              username,
              password,
            },
            goHome: () => {
              Router.replace('/home')
            },
          }
          // 登录请求发送
          store.dispatch('requestLogin', data)
        } else {
          Message.error('请填写账号或密码')
        }
      })
    }

    return {
      ...toRefs(state),
      nameClass,
      passClass,
      habdleUserName,
      habdleUserPass,
      loginForm,
      submitForm,
    }
  },
}
</script>

<style scoped lang="scss">
.instructions {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  letter-spacing: 2px;
  pointer-events: none;
}

canvas {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
  width: 100%;
  height: 100vh;
}
.login-container {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background: #000;
  .login-sider-container {
    position: relative;
    width: 100%;
    height: 100vh;
    .sider-header {
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 48px;
        height: 48px;
        margin-right: 12px;
      }
      .eleplus {
        width: 128px;
        height: 48px;
      }
    }
    .sider-cent {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: left;
      border-radius: 4px;
      position: relative;
      z-index: 1;
      background: rgba(255, 255, 255, 0.9);
      padding: 40px 45px 20px;
      width: 450px;
      box-shadow: 0 20px 50px 0 hsl(0deg 0% 64% / 10%);
      .el-form-item {
        position: relative;
        ::v-deep .el-input__inner {
          padding-right: 160px;
        }
        span {
          transition: left 0.3s ease-in;
          position: absolute;
          left: 20px;
          top: 50%;
          transform: translateY(-50%);
          font-size: 16px;
          color: #5d9aff;
          z-index: 1;
          &.name-right {
            left: 310px;
            font-size: 12px;
          }
          &.pass-right {
            left: 350px;
            font-size: 12px;
          }
        }
      }
      .submit-btn {
        width: 100%;
      }
    }
  }
}
</style>
